<template>
  <div id="detail">
    <detail-nav-bar class="detail-nav" @titleClick="titleClick" ref="nav" />
    <scroll
      class="content"
      :probe-type="3"
      :pull-up-load="true"
      ref="scroll"
      @scroll="contentScroll"
    >
      <ul>
        <li v-for="item in $store.state.cartList">
          {{ item }}
        </li>
      </ul>
      <detail-swiper :top-images="topImages" />
      <detail-base-info :goods="goodsInfo" />
      <detail-shop-info :shop="shop" />
      <detail-goods-info :detail-info="detailInfo" @imageLoad="imageLoad" />
      <detail-param-info :params-info="paramsInfo" ref="params" />
      <detail-comment-info :comment-info="commentInfo" ref="comment" />
      <goods-list :goods="recommend" ref="recommend" />
    </scroll>
    <back-top @click.native="bakClick()" v-show="isShowBackTop" />
    <detail-bottom-bar @addCart="addToCart" />
    <!-- <toast :message="message" :show="show"/> -->
  </div>
</template>

<script>
import DetailShopInfo from "./childComps/DetailShopInfo.vue";
import DetailBaseInfo from "./childComps/DetailBaseInfo.vue";
import DetailSwiper from "./childComps/DetailSwiper.vue";
import DetailNavBar from "./childComps/DetailNavBar.vue";
import DetailGoodsInfo from "./childComps/DetailGoodsInfo.vue";
import DetailParamInfo from "./childComps/DetailParamInfo.vue";
import DetailCommentInfo from "./childComps/DetailCommentInfo.vue";
import DetailBottomBar from "./childComps/DetailBottomBar.vue";

import BackTop from "components/content/backTop/BackTop.vue";
import Scroll from "components/common/scroll/Scroll.vue";
// import Toast from "components/common/toast/Toast.vue";

import GoodsList from "common/GoodsList.vue";
import {
  getDetail,
  Goods,
  Shop,
  GoodsParam,
  getRecommend,
} from "network/detail";

// import { itemListenerMixin, backTopMixin } from "common/mixin";
import { debounce } from "common/utils";
import axios from "axios";

import { mapActions } from "vuex";
export default {
  name: "Detail",
  components: {
    DetailNavBar,
    DetailSwiper,
    DetailBaseInfo,
    DetailShopInfo,
    Scroll,
    DetailGoodsInfo,
    DetailParamInfo,
    DetailCommentInfo,
    GoodsList,
    DetailBottomBar,
    BackTop,
    // Toast,
  },
  data() {
    return {
      iid: null,
      topImages: [],
      goodsInfo: {},
      // goods:{},
      shop: {},
      detailInfo: {},
      paramsInfo: {},
      commentInfo: {},
      recommend: [],
      themeTopYs: [],
      currentIndex: 0,
      isShowBackTop: false,
      message: "",
      show: false,
    };
  },
  created() {
    // 1.保存传入的id
    this.iid = this.$route.query.iid;
    // this.iid = this.$route.params.iid;

    // 2.根据iid请求详情数据
    getDetail(this.iid).then((res) => {
      //1.获取顶部的图片轮播数据
      this.topImages = res.data.result.itemInfo.topImages;
      // console.log(res.data.result);

      //2.创建商品的对象，获取商品信息
      this.goodsInfo = new Goods(
        res.data.result.itemInfo,
        res.data.result.columns,
        res.data.result.shopInfo.services
      );
      // this.goods=res.data.result
      //3.创建店铺信息的对象
      this.shop = new Shop(res.data.result.shopInfo);

      //4.取出商品详情数据
      this.detailInfo = res.data.result.detailInfo;

      //5.获取参数信息
      // this.paramsInfo=new GoodsParam(res.data.result.itemParams.info,res.data.result.itemParams.rule)
      this.paramsInfo = res.data.result.itemParams;

      //6.取出评论信息
      if (res.data.result.cRate !== 0) {
        this.commentInfo = res.data.result.rate.list[0];
      }
    });

    //3.请求推荐数据
    getRecommend().then((res) => {
      // console.log(res.data.data.list);
      this.recommend = res.data.data.list;
    });
  },
  mounted() {
    //图片加载完成的事件监听
    this.bus.$on("itemImgLoad", () => {
      this.$refs.scroll.refresh();
    });
  },
  destroyed() {
    this.$bus.$off("itemImgLoad");
  },
  methods: {
    ...mapActions(["addCart"]),
    imageLoad() {
      this.$refs.scroll.refresh();
      this.themeTopYs = [];
      this.themeTopYs.push(0);
      this.themeTopYs.push(this.$refs.params.$el.offsetTop);
      this.themeTopYs.push(this.$refs.comment.$el.offsetTop);
      this.themeTopYs.push(this.$refs.recommend.$el.offsetTop);
      this.themeTopYs.push(Number.MAX_VALUE);
      console.log(this.themeTopYs);
    },
    //跳转到页内对应模块
    titleClick(index) {
      this.$refs.scroll.scrollTo(0, -this.themeTopYs[index], 200);
    },

    //tab滚动tab选中
    contentScroll(position) {
      //1.获取y值
      const positionY = -position.y;
      //2.positionY和主题中值进行对比
      //[0, 6506, 7308, 7525,MAX_VALUE]
      //positionY在0和6506之间,index=0  6506和7308之间,index=1  7308和7525之间,index=2  超过7525,index=3
      let length = this.themeTopYs.length;
      //hack做法
      for (let i = 0; i < length - 1; i++) {
        if (
          this.currentIndex !== i &&
          positionY >= this.themeTopYs[i] &&
          positionY < this.themeTopYs[i + 1]
        ) {
          this.currentIndex = i;
          this.$refs.nav.currentIndex = this.currentIndex;
        }
      }
      //普通做法
      // for (let i = 0; i < length; i++) {
      //   if (
      //     (this.currentIndex !== i &&
      //       i < length - 1 &&
      //       positionY >= this.themeTopYs[i] &&
      //       positionY < this.themeTopYs[i + 1]) ||
      //     (i === length - 1 && positionY >= this.themeTopYs[i])
      //   ) {
      //     this.currentIndex = i;
      //     this.$refs.nav.currentIndex = this.currentIndex;
      //   }
      // }

      //3.是否显示‘回到顶部’
      this.isShowBackTop = -position.y > 1000;
    },
    bakClick() {
      this.$refs.scroll.scrollTo(0, 0, 300);
      console.log("返回顶部");
    },
    addToCart() {
      //1.获取购物车需要展示的信息
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goodsInfo.title;
      product.desc = this.goodsInfo.desc;
      product.price = this.goodsInfo.realPrice;
      product.iid = this.iid;

      //2.将商品添加到购物车
      this.addCart(product).then((res) => {
        // this.show=true;
        // this.message=res;
        // setTimeout(() => {
        //   this.show=false;
        //   this.message=''
        // }, 1500);
        this.$toast.show(res)
      });

      // this.$store.dispatch("addCart", product).then((res) => {
      //   console.log(res);
      // });

      // this.$store.commit("addCart", product);
    },

    // getDetail(iid) {
    //   axios({
    //     url: "http://152.136.185.210:7878/api/hy66/detail",
    //     params: {
    //       iid: iid,
    //     },
    //   }).then((res) => {
    //     console.log(res);
    //   });
    // },
  },
};
</script>

<style scoped>
#detail {
  height: 100vh;
}
.content {
  height: calc(100% - 44px);
  overflow: hidden;
}
</style>
